/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.swt.mto.thithu.service.impl;

import java.util.Date;
import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.swt.mto.thithu.model.Question;
import com.swt.mto.thithu.service.base.QuestionLocalServiceBaseImpl;

/**
 * The implementation of the question local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.swt.mto.thithu.service.QuestionLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author tuan.nguyen
 * @see com.swt.mto.thithu.service.base.QuestionLocalServiceBaseImpl
 * @see com.swt.mto.thithu.service.QuestionLocalServiceUtil
 */
public class QuestionLocalServiceImpl extends QuestionLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * com.swt.mto.thithu.service.QuestionLocalServiceUtil} to access the
	 * question local service.
	 */
	public Question addQuestion(Long questionId, Long createUserId,
			Long modifyUserId, Long quizId, String title, String description)
			throws PortalException, SystemException {

		Date now = new Date();
		Question question = null;
		if (questionId == null) {
			questionId = counterLocalService.increment();
			question = questionPersistence.create(questionId);
			question.setCreateDate(now);
		} else {
			question = questionPersistence.fetchByPrimaryKey(questionId);
			question.setModifydate(now);
		}

		question.setQuizId(quizId);
		question.setCreateUserId(createUserId == null ? 0 : createUserId);
		question.setModifyUserId(modifyUserId == null ? 0 : modifyUserId);
		question.setTitle(title);
		question.setDescription(description);

		questionPersistence.update(question, false);
		return question;
	}
	
	public List<Question> getQuestionByQuiz(long quizId){
		List<Question> questions = null;
		try {
			questions = questionPersistence.findByQuizId(quizId);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return questions;
	}
}